package com.tidy.seed.admin.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tidy.seed.admin.system.dto.AddMenuIdsByRoleIdDTO;
import com.tidy.seed.admin.system.entity.RoleMenu;
import com.tidy.seed.admin.system.mapper.RoleMenuMapper;
import com.tidy.seed.admin.system.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tidy.seed.admin.system.vo.FindMenuIdsByRoleIdVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HZ-ZY
 * @since 2025-08-06
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<FindMenuIdsByRoleIdVO> findMenuIdsByRoleId(Integer roleId) {
        return roleMenuMapper.findMenuIdsByRoleId(roleId);
    }

    @Override
    public Boolean addMenuIdsByRoleId(AddMenuIdsByRoleIdDTO dto) {
        //删除所有再新增
        roleMenuMapper.delete(Wrappers.<RoleMenu>query().eq("role_id", dto.getRoleId()));
        List<RoleMenu> roleMenuList = Arrays.stream(dto.getMenuIds())
                .map(menuId -> {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(dto.getRoleId());
                    roleMenu.setMenuId(menuId);
                    return roleMenu;
                })
                .collect(Collectors.toList());
        return this.saveBatch(roleMenuList);
    }
}
